package com.sunny.ynhz.ui.report;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ChooseSubmitDutyAdapter;
import com.sunny.ynhz.adapter.ChooseSubmitObjectAdapter;
import com.sunny.ynhz.bean.RiverChiefBeanMsg;
import com.sunny.ynhz.bean.RiverDutyBean;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.RiverChiefModel;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huhuan123 on 2018/9/16.
 *
 * @describe TODO
 */

public class RiverDutyFragment extends BaseFragment implements IProblemTypeResultView<RiverDutyBean> {
    @BindView(R.id.lv_current_river)
    RecyclerView rv_current_river;
    @BindView(R.id.ed_search_river)
    EditText editText;
    @BindView(R.id.ll_choose)
    LinearLayout ll_choose;
    List<RiverDutyBean.DateBean> mList = new ArrayList<>();
    List<RiverDutyBean.DateBean> mAllList = new ArrayList<>();
    private ChooseSubmitDutyAdapter submitObjectAdapter;
    private RiverChiefModel riverChiefModel;

    public static RiverDutyFragment getRiverDutyFragment() {
        return new RiverDutyFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.rv_river_msg;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        editText.setHint("请输入职能部门");
        ll_choose.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_current_river.setLayoutManager(linearLayoutManager);
        submitObjectAdapter = new ChooseSubmitDutyAdapter(mList);
        rv_current_river.setAdapter(submitObjectAdapter);
        riverChiefModel = new RiverChiefModel(getContext(), this);
        riverChiefModel.getRiverDutyData(1, true);
        submitObjectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RiverDutyBean.DateBean dateBean = mList.get(position);
                String name = dateBean.getNAME();
                String id = dateBean.getOFFICE_ADMIIN();
                Intent intent = new Intent();
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                getActivity().setResult(500, intent);
                getActivity().finish();
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String name = charSequence.toString();
                if (!TextUtils.isEmpty(name)) {
                    List<RiverDutyBean.DateBean> dateBeans = riverChiefModel.serachDuty(mList, name);
                    mList.clear();
                    mList.addAll(dateBeans);
                } else {
                    mList.clear();
                    mList.addAll(mAllList);
                }
                submitObjectAdapter.notifyDataSetChanged();


            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


    }

    @Override
    public void onSuccess(RiverDutyBean riverChiefBeanMsg) {
        mList.addAll(riverChiefBeanMsg.getDate());
        if (mList.size() == 0) {
            loadEmply();
        } else {
            loadSuccess();
        }
        submitObjectAdapter.notifyDataSetChanged();
        mAllList.addAll(mList);
    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        loadFail();

    }

    @Override
    public void onStartLoad() {
        startLoad();
    }

    @Override
    public void resLoad() {
        super.resLoad();
        riverChiefModel.getRiverDutyData(1, true);
    }
}
